package com.hk.jmm.helper;

import android.text.TextUtils;
import android.util.Log;

import com.hk.jmm.db.GreenDaoManager;
import com.hk.jmm.db.UserInfo;
import com.hk.jmm.gen.UserInfoDao;
import com.skyz.base.application.BaseApplication;
import com.skyz.wrap.manager.TokenManager;

import java.util.List;

public class UserHelper {
    private UserHelper() {

    }

    static UserHelper instance = new UserHelper();

    public static UserHelper getInstance() {
        return instance;
    }

    public UserInfo getCurUserInfo() {
        String userName = TokenManager.getInstance().getUserName(BaseApplication.getApplication());
        return getUserInfoByUserName(userName);
    }

    public String getCurUserName() {
        UserInfo userInfo = getCurUserInfo();
        if (userInfo == null) {
            return "";
        }
        return userInfo.getNickName();
    }

    public String getAccount() {
        UserInfo userInfo = getCurUserInfo();
        if (userInfo == null) {
            return "";
        }
        return userInfo.getUserName();
    }

    public String getOpenId() {
        UserInfo userInfo = getCurUserInfo();
        if (userInfo == null) {
            return "";
        }
        return userInfo.getOpenId();
    }

    public String getCurUserID() {
        UserInfo userInfo = getCurUserInfo();
        if (userInfo == null) {
            return "";
        }
        return userInfo.getUserId();
    }

    public UserInfo getUserInfoByUserName(String userName) {
        UserInfoDao userInfoDao = GreenDaoManager.getInstance().getNewSession().getUserInfoDao();
//        List users = userInfoDao.queryBuilder().list();
//        Log.e("user size", users.size() + "");
        List<UserInfo> userInfos = userInfoDao.queryBuilder().where(UserInfoDao.Properties.UserName.eq(userName)).list();
        if (userInfos != null && userInfos.size() > 0) {
            return userInfos.get(0);
        }
        return null;
    }

    /**
     * 根据opendId查询用户信息
     *
     * @param openId
     * @return
     */
    public UserInfo getUserInfoByOpenId(String openId) {
        UserInfoDao userInfoDao = GreenDaoManager.getInstance().getNewSession().getUserInfoDao();
        List<UserInfo> userInfos = userInfoDao.queryBuilder().where(UserInfoDao.Properties.OpenId.eq(openId)).list();
        if (userInfos != null && userInfos.size() > 0) {
            return userInfos.get(0);
        }
        return null;
    }

    public void updateUserInfo(List<UserInfo> userList) {
        UserInfoDao userInfoDao = GreenDaoManager.getInstance().getNewSession().getUserInfoDao();
        if (userList == null) {
            return;
        }
        for (UserInfo user : userList) {
            userInfoDao.insertOrReplaceInTx(user);
        }
    }

    public List<UserInfo> getCurrentUserList() {
        UserInfoDao userInfoDao = GreenDaoManager.getInstance().getNewSession().getUserInfoDao();
        List users = userInfoDao.queryBuilder().list();
        return users;
    }


    /**
     * personForm对象转用户对象
     */
    public static UserInfo personFormToUserForm(String openId) {
        UserInfo userInfo = null;
        if (!TextUtils.isEmpty(openId)) {
            userInfo = UserHelper.getInstance().getUserInfoByOpenId(openId);
        }
        return userInfo == null ? new UserInfo() : userInfo;
    }


    public UserInfo getUserInfoByQrcode(String userName) {
        UserInfoDao userInfoDao = GreenDaoManager.getInstance().getNewSession().getUserInfoDao();
        List<UserInfo> users = userInfoDao.queryBuilder().where(UserInfoDao.Properties.UserName.eq(userName)).list();
        if (users == null || users.size() == 0) {
            return null;
        }
        return users.get(0);
    }
}
